//
//  MessageListViewController.m
//  Learn
//
//  Created by zyl_macbookpro on 2024/4/1.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "MessageListViewController.h"
#import "CustomizeProgressView.h"
#import "MessageListTableViewCell.h"
#import "MineAPIManager.h"
@interface MessageListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic ,strong)UITableView *tableView;

@property(nonatomic ,strong)NSArray *titleArray;

@property(nonatomic ,strong)UIView *tableFootorView;

@property(nonatomic ,strong)UIButton *finishedBtn;

@property(nonatomic ,strong)UIView *tableHeaderView;

@property(nonatomic ,strong)NSMutableArray *dataArray;

@end

@implementation MessageListViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self renderNavBarWithBackgroundImage:[UIImage imageWithColor:lClearColor] backgroundColor:lClearColor shadowColor:nil textAttributeDic:@{NSForegroundColorAttributeName:rgba(51, 51, 51, 1),NSFontAttributeName:MFontWithSize(18)} enableScrollEdgeAppearance:NO];
    [self loadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"消息";
    // Do any additional setup after loading the view.
   
    [self setBackButtonIsHidden:NO];
    [self.view addSubview:self.tableView];
    self.view.backgroundColor = rgba(245, 245, 245, 1);
    self.finishedBtn.superview.y = self.tableView.height;
   
}
- (void)loadData
{
    self.dataArray = [NSMutableArray arrayWithCapacity:0];
    [MineAPIManager getMessageListWithParam:@{} result:^(NSDictionary * _Nonnull dic) {
       // NSLog(@"UUUUUUUUtUUUU%@",[dic modelToJSONString]);
        NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithCapacity:0];
        NSArray *arr1 = dic[@"data"][@"message"];
        if (arr1.count) {
            NSDictionary *dic = arr1.firstObject;
            [dic1 setObject:dic[@"create_time"] forKey:@"date"];
            [dic1 setObject:dic[@"content"] forKey:@"content"];
        }
        [dic1 setObject:@"作业消息" forKey:@"title"];
        [dic1 setObject:[NSNumber numberWithInt:[dic[@"data"][@"message_not_read"] intValue]] forKey:@"msg_number"];
        [dic1 setObject:dic[@"data"][@"message"] forKey:@"msg_list"];
        [dic1 setObject:@"ic_message_notic" forKey:@"icon"];
        NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithCapacity:0];
        [dic2 setObject:[NSNumber numberWithInt:[dic[@"data"][@"notice_not_read"] intValue]] forKey:@"msg_number"];
        [dic2 setObject:dic[@"data"][@"notice"] forKey:@"msg_list"];
        
        NSArray *arr2 = dic[@"data"][@"notice"];
        if (arr2.count) {
            NSDictionary *dic = arr2.firstObject;
            [dic2 setObject:dic[@"create_time"] forKey:@"date"];
            [dic2 setObject:dic[@"content"] forKey:@"content"];
        }
        [dic2 setObject:@"站内信" forKey:@"title"];
        [dic2 setObject:@"ic_message_msg" forKey:@"icon"];
        [self.dataArray addObject:dic1];
        [self.dataArray addObject:dic2];
        
        [self.tableView reloadData];
    }];
}
#pragma mark -UITableViewDelegate,UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageListTableViewCell"];
    
    if (!cell)
    {
        cell = [[MessageListTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"MessageListTableViewCell"];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    cell.data = self.dataArray[indexPath.row];
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 76;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UIViewController *vc = [[UIViewController alloc] initWithClassName:@"MessageDetailViewController"];
    vc.paraObject = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:true];
    
}
#pragma mark -getters and setters
-(UITableView *)tableView{
    if (!_tableView){
        _tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, lTopBarHeight, lScreenWidth, lScreenHeight - lTopBarHeight) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        _tableView.bounces = NO;
        [_tableView setBackgroundColor:lClearColor];
        [_tableView setSeparatorColor:rgba(229, 229, 229, 1)];
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 10, 0, 0)];
        
    }
    
    return _tableView;
}

//-(UIButton *)finishedBtn
//{
//    if (!_finishedBtn)
//    {
//        _finishedBtn = [UIButton new];
//        _finishedBtn.frame =CGRectMake(0, 0, lScreenWidth - 20, 45);
//       
//        _finishedBtn.selected =NO;
//        [_finishedBtn setTitle:@"完成" forState:UIControlStateNormal];
//        [_finishedBtn setTitleColor:lWhiteColor forState:UIControlStateNormal];
//        [_finishedBtn.titleLabel setFont:MFontWithSize(18)];
//        [_finishedBtn addTarget:self action:@selector(finishedBtnAction) forControlEvents:UIControlEventTouchUpInside];
//        _finishedBtn.backgroundColor = lClearColor;
//        UIView *btnView =[[UIView alloc] initWithFrame:CGRectMake(10, 65, lScreenWidth - 20, 45)];
//        [btnView setAllCornersWithCornerRadius:22.5];
//        // gradient
//        CAGradientLayer *gl = [CAGradientLayer layer];
//        gl.frame = CGRectMake(0,0,lScreenWidth - 20,45);
//        gl.startPoint = CGPointMake(1, 0.5);
//        gl.endPoint = CGPointMake(0, 0.5);
//        gl.colors = @[(__bridge id)[UIColor colorWithRed:41/255.0 green:241/255.0 blue:156/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:2/255.0 green:161/255.0 blue:249/255.0 alpha:1.0].CGColor];
//        gl.locations = @[@(0), @(1.0f)];
//        [btnView.layer addSublayer:gl];
//        [btnView addSubview:self.finishedBtn];
//        [self.view addSubview:btnView];
//    }
//    
//    return _finishedBtn;
//}
-(UIView *)tableHeaderView
{
    if (!_tableHeaderView)
    {
        
        _tableHeaderView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth,72)];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, lScreenWidth - 20, 22)];
        titleLabel.font = MFontWithSize(16);
        titleLabel.text = @"字体设置大小";
        [titleLabel setTextAlignment:NSTextAlignmentLeft];
        titleLabel.textColor = rgba(142, 142, 142, 1);
        [_tableHeaderView addSubview:titleLabel];
        CustomizeProgressView *fontView = [[CustomizeProgressView alloc] initWithFrame:CGRectMake(20, titleLabel.bottom + 20, lScreenWidth - 40, 34) titleArray:@[@"偏小",@"标准",@"",@"超大"] Progress:0.75];
        fontView.block = ^(double progress) {
            NSLog(@"HHHHHHHHHHH%f",progress);
        };
        [_tableHeaderView addSubview:fontView];
        
        UILabel *titleLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(10, fontView.bottom + 30, lScreenWidth - 20, 22)];
        titleLabel2.font = MFontWithSize(16);
        titleLabel2.text = @"声音播放速度";
        [titleLabel2 setTextAlignment:NSTextAlignmentLeft];
        titleLabel2.textColor = rgba(142, 142, 142, 1);
        [_tableHeaderView addSubview:titleLabel2];
        CustomizeProgressView *voiceView = [[CustomizeProgressView alloc] initWithFrame:CGRectMake(20, titleLabel2.bottom + 20, lScreenWidth - 40, 34) titleArray:@[@"慢速",@"正常",@"快速"] Progress:0.75];
        fontView.block = ^(double progress) {
            NSLog(@"HHHHHHHHHHH%f",progress);
        };
        [_tableHeaderView addSubview:voiceView];
        _tableHeaderView.height = voiceView.bottom + 30;
        [_tableHeaderView addSubview:separateLineWithColor(CGRectMake(0, _tableHeaderView.height - 1, lScreenWidth, 1), rgba(229, 229, 229, 1))];
        
    }
    
    return _tableHeaderView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
